import { TileClassifier } from "@/utils/TileClassifier.js";
export class TileAutoDecoder {

    
    static decode(tileId) {
        if (TileClassifier.isTileA1(tileId)) {
            return this.decodeA1(tileId);
        } else if (TileClassifier.isTileA2(tileId)) {
            return this.decodeA2(tileId);
        } else if (TileClassifier.isTileA3(tileId)) {
            return this.decodeA3(tileId);
        } else if (TileClassifier.isTileA4(tileId)) {
            return this.decodeA4(tileId);
        }
        return {};
    }

    static decodeA1(tileId) {
        let kind = TileClassifier.getAutotileKind(tileId);
        let autotileTable=null;
        let animX=0;
        let animY=0;
        let baseCol=0;
        let baseRow=0;
        /**
         * kind的值取0~15，可以观察A1图(作为动画的三个并列组合图为一种图块)
         * A1图以相邻4种图为块排序
         * 第一区域的排序为
         * 0 2
         * 1 3
         * 
         * 其余区域排序为
         * 0 1
         * 2 3
         */
        if (kind === 0) {
            animX = 2;
            baseRow = 0;
        } else if (kind === 1) {
            animX = 2;
            baseRow = 3;
        } else if (kind === 2) {
            baseCol = 6;
            baseRow = 0;
        } else if (kind === 3) {
            baseCol = 6;
            baseRow = 3;
        } else {

            /**
             * baseCol，baseRow的取值计算应该是
             * 先将A1图片以相邻的4种图块为单位(可以观察A1图,里面动画的三个并列图为一种图块)
             * 计算改区域的索引
             * gIndex=kind/4;
             * 获取需绘制的图块的区域位置
             * ax=gIndex%2;
             * ay=gIndex/2;
             * 获取需绘制图块在区域的索引
             * aIndex= kind%4;
             * 获取需绘制图块在区域的位置
             * llx=aindex%2;
             * lly=aindex/2;
             * 计算需绘制图块在原图的位置
             * baseCol=ax*8;
             * baseRow=ay*6+lly*3;
             * 如果是瀑布将bx+6就行(瀑布前有6个48px的块)
             * 
             * 化简就是下面结果
             */
            baseCol = (kind & 0x04) << 1;
            baseRow = (((kind & 0x08) >> 2) | ((kind & 0x02) >> 1)) * 3;
            if (kind & 0x01) {
                baseCol += 6;
                autotileTable = TileClassifier.WATERFALL_AUTOTILE_TABLE;
                animY = 1;
            }else{
                animX = 2;
            }
        }
        return {
            setNumber:0,
            autotileTable,
            baseCol,
            baseRow,
            animX,
            animY
        }
    }

    static decodeA2(tileId){
        let kind = TileClassifier.getAutotileKind(tileId);
        let baseCol = (kind&0x07)<<1;
        //这里减2是因为rpgmakermv中地图块编辑器，前两行被A1图块占据其他图块占据
        let baseRow = ((kind>>3)-2) * 3;
        let isTable = TileClassifier.isTableTile(tileId);
        return {
            setNumber:1,
            baseCol,
            baseRow,
            isTable
        };
    }

    static decodeA3(tileId){
        let kind = TileClassifier.getAutotileKind(tileId);
        let baseCol = (kind&0x07) * 2;
        let baseRow = ((kind>>3) - 6)*2;
        let autotileTable = TileClassifier.WALL_AUTOTILE_TABLE;
        return {
            setNumber:2,
            baseCol,
            baseRow,
            autotileTable
        };
    }

    static decodeA4(tileId){
        let kind = TileClassifier.getAutotileKind(tileId);
        let autotileTable=null;
        let tx=kind&0x07;
        let ty=kind>>3;
        let baseCol = tx*2;
        let baseRow = Math.floor((ty - 10) * 2.5 + (ty % 2 === 1 ? 0.5 : 0));
        if (tx%2 == 1) {
            autotileTable = Tilemap.WALL_AUTOTILE_TABLE;
        }

        return {
            autotileTable,
            setNumber:3,
            baseCol,
            baseRow
        }
    }
}